//
//  TutorialLayer.cpp
//  Space shooting
//
//  Created by TungNguyen on 9/24/13.
//
//

#include "TutorialLayer.h"
#include "StateManager.h"
#include "Constants.h"
#include "CCControlButton.h"
#include "CCScrollView.h"

using namespace cocos2d;

TutorialLayer::TutorialLayer() {    
    init();
}

TutorialLayer::~TutorialLayer() {
    removeAllChildrenWithCleanup(true);
}

bool TutorialLayer::init() {
    
    if(!CCLayer::init()) {
        
        return false;
    }
    
    CCSize size = CCDirector::sharedDirector()->getWinSize();
    
    addChild(CCLayerColor::create(ccc4(122, 144, 0, 255), size.width, size.height));
    
    // Tutorial title
    CCLabelTTF *tutorialLabel = CCLabelTTF::create("Tutorial", "Spac3 neon.ttf", 30);
    tutorialLabel->setPosition(ccp(size.width/2, size.height - tutorialLabel->getTexture()->getContentSize().height));
    tutorialLabel->setColor(ccc3(255, 109, 1));
    addChild(tutorialLabel);
    
    
    CCMenu *menu= CCMenu::create();
    menu->setPosition(CCPointZero);
    
    CCMenuItemImage *homeButton = CCMenuItemImage::create("Icon-Small.png", "Icon-Small.png", this, menu_selector(TutorialLayer::menuItemHome));
    homeButton->setPosition(ccp(size.width - 30, size.height - 30));
    menu->addChild(homeButton);
    
    CCMenuItemImage *preButton = CCMenuItemImage::create("pre.png", "pre_selected.png", this, menu_selector(TutorialLayer::menuItemPre));
    preButton->setPosition(ccp(size.width/5, 40));
    menu->addChild(preButton);
    
    CCMenuItemImage *nextButton = CCMenuItemImage::create("next.png", "next_selected.png", this, menu_selector(TutorialLayer::menuItemNext));
    nextButton->setPosition(ccp(size.width - size.width/5, 40));
    menu->addChild(nextButton);
    
    addChild(menu);
    
    // Init Array Image
    arrayImage = new CCArray();
    arrayImage->addObject( CCString::create("tut1.png"));
    arrayImage->addObject( CCString::create("tut2.png"));
    arrayImage->addObject( CCString::create("tut3.png"));
    
    currentIndex = 0;
    
    currentImg = (CCString *)arrayImage->objectAtIndex(currentIndex);
   
    imgTutorial = CCSprite::create(currentImg->getCString());
    imgTutorial->setScale(0.7);
    imgTutorial->setPosition(ccp(size.width/2, size.height/2));
    addChild(imgTutorial);
 
   
    return true;
}

void TutorialLayer::menuItemNext(CCObject *object)  {
    
    currentIndex++;
    if(currentIndex >= arrayImage->count()) {
        currentIndex = arrayImage->count() - 1;
    }
   
    currentImg = (CCString *)arrayImage->objectAtIndex(currentIndex);
    imgTutorial->setTexture(CCTextureCache::sharedTextureCache()->addImage(currentImg->getCString()));
    
}

void TutorialLayer::menuItemPre(CCObject *object)  {
    
    currentIndex--;
    
    if(currentIndex <= 0) {
        currentIndex = 0;
    }

    currentImg = (CCString *)arrayImage->objectAtIndex(currentIndex);
    imgTutorial->setTexture(CCTextureCache::sharedTextureCache()->addImage(currentImg->getCString()));
}

void TutorialLayer::menuItemHome(CCObject *object)  {
    StateManager::getInstance()->switchScene(GAME_STATE_HOME);
}
